﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Micrhard.FMCG.Server.Interface;
using Microhard.Core.DBUtility;
using Micrhard.FMCG.Server.DataAccess;
using Micrhard.FMCG.Server.DTO;
using Micrhard.FMCG.Server.Msg;
using Microhard.Core;
using Micrhard.FMCG.Server.Models;

namespace Micrhard.FMCG.Server.Impl
{
    public class OrderWorkSheetImpl : IOrderWorkSheet
    {
        public Microhard.Core.DBUtility.ExecutedResult Add(Models.OrderWorkSheetModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;
            op.RegEmpID = this.scope.EmpInfo.EmployeeID;
            //从ExecuteTypeDisplay取值
            Guid et = Guid.Parse(model.ExecuteTypeDisplay);
            op.ExecuteType = et;

            //修改订单状态为处理中
            var order = db.Orders.SingleOrDefault(s => s.OrderID == model.OrderID);
            order.Status = (int)OrderStatus.Checked;


            try
            {
                db.OrderWorkSheets.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, OrderWorkSheetMsg.AddFailed);
                Log.Error("IOrderWorkSheet.Add", ex);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.OrderWorkSheetModel model)
        {
            ExecutedResult result = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var entity = db.OrderWorkSheets.SingleOrDefault(l => l.OrderExecuteID == model.OrderExecuteID);
            if (entity != null)
            {
                entity.Name = model.Name;
                entity.ExecuteContent = model.ExecuteContent;
                entity.ExecuteDate = model.ExecuteDate;
                entity.ExecuteResult = model.ExecuteResult;
                entity.ExpressNo = model.ExpressNo;
                entity.RealRecCost = model.RealRecCost;
                entity.RecCostDate = model.RecCostDate;
                entity.Status = model.Status;

                //从ExecuteTypeDisplay取值
                Guid et = Guid.Parse(model.ExecuteTypeDisplay);
                entity.ExecuteType = et;

                if (model.Status)
                {
                    //修改订单状态为处理完毕
                    entity.Order.Status = (int)OrderStatus.Finished;
                }
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, OrderWorkSheetMsg.UpdateFailed);
                Log.Error("IOrderWorkSheet.Update", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var entity = db.OrderWorkSheets.Where(l => ids.Contains(l.OrderExecuteID));
            foreach (var e in entity)
            {
                //工单是否关闭
                if (e.Status == false)
                {
                    db.OrderWorkSheets.DeleteObject(e);
                }
                else
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, OrderWorkSheetMsg.SheetIsClose);
                }
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, CompanyMsg.DeleteFailed);
                    Log.Error("IOrderWorkSheet.Delete", ex);
                }
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.OrderWorkSheetModel model)
        {
            throw new NotImplementedException();
        }

        public Models.OrderWorkSheetModel GetModelByID(Guid id)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var model = db.Orders.SingleOrDefault(c => c.OrderID == id);
            if (model != null)
            {
                OrderWorkSheetModel rtn = new OrderWorkSheetModel();
                rtn.Order = model.ToModel();
                rtn.OrderID = id;
                return rtn;
            }
            return null;
        }

        public Models.OrderWorkSheetModel GetModelByModel(Models.OrderWorkSheetModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.OrderWorkSheetModel> Query(Models.OrderWorkSheetModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            IEnumerable<OrderWorkSheet> entities = from b in db.OrderWorkSheets
                                                   where b.OrderID == Model.OrderID
                                                   && b.Order.CompanyID == this.scope.EmpInfo.CompanyID
                                                   select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<OrderWorkSheetModel>();
        }

        public List<Models.OrderWorkSheetModel> Query(Models.OrderWorkSheetModel Model)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            IEnumerable<OrderWorkSheet> entities = from b in db.OrderWorkSheets
                                                   where b.OrderID == Model.OrderID
                                                   && b.Order.CompanyID == this.scope.EmpInfo.CompanyID
                                                   select b;
            if (entities.Count() > 0)
                return entities.OrderByDescending(p => p.CreateTime).Select(m => m.ToModel()).ToList();
            return new List<OrderWorkSheetModel>();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
